<?php

namespace app\admin\controller;

use app\admin\model\Admin;
use app\admin\model\User;
use app\common\controller\Backend;
use app\common\model\Attachment;
use fast\Date;
use think\Db;

/**
 * 控制台
 *
 * @icon   fa fa-dashboard
 * @remark 用于展示当前系统中的统计数据、统计报表及重要实时数据
 */
class Dashboard extends Backend
{
    protected $noNeedRight = ['*'];
    protected $noNeedLogin = ['*'];

    public function _initialize()
    {
        parent::_initialize();
        $this->db2 = Db::connect(config('database.db2'));
    }

    public function user_visit_update()
    {
        $user = Db::name('user')->field('id')->select();
        foreach ($user as $key => $value) {
            $user_visit_log = Db::name('user_visit_log')->where('user_id',$value['id'])->order('id DESC')->find();
            if ($user_visit_log) {

            unset($user_visit_log['id']);
            // 写数据
            Db::name('user_visit_update')->insert($user_visit_log);
            }
        }
    }

    /**
     * 查看
     */
    public function index()
    {
        $city = input('city','');
        $date = input('date','');
        $salesman = input('salesman','');
        // 当前用户的权限
        if ($salesman) {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman])->find();
            $groups = [];
            if ($auth_group_access['group_id']==3) { // 业务员
                $groups[] = $salesman;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $salesman;
                $admin = $this->db2->name('admin')->where(['leader'=>$salesman])->find();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        } else {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$this->auth->id])->find();
            $groups = [];
            if ($auth_group_access['group_id']==3) { // 业务员
                $groups[] = $this->auth->id;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $this->auth->id;
                $admin = $this->db2->name('admin')->where(['leader'=>$this->auth->id])->find();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        }
        $where_group= [];
        $where_group1= [];
        $where_group2= [];
        $where_group3= [];
        if ($groups) {
            $where_group['service_admin_id'] = ['in',$groups];
            $where_group1['p.service_admin_id'] = ['in',$groups];// 涉及到业务员交接，用活动的字段查询
            $where_group2['salesman_admin_id'] = ['in',$groups];
            $where_group3['s.salesman_admin_id'] = ['in',$groups];
        }
        $where_city = [];
        $where_city1 = [];
        if ($city) {
            $citys = explode('/', $city);
            if (count($citys)==3) {
                $end = end($citys);
                $where_city['area'] = $end;
                $where_city1['s.area'] = $end;
            } elseif (count($citys)==2) {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            } else {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            }
        }
        // 今日概况
        $time = time();
        $start_date = date('Y-m-d',$time);
        $end_date = date('Y-m-d H:i:s',$time);
        $start_time = strtotime($start_date);
        $end_time = $time;
        if ($date) {
            list($start_date,$end_date) = explode(' - ', $date);
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
        }
        $where1 = 'jointime>='.$start_time.' and jointime<'.$end_time;
        $where2 = 'create_dt>="'.$start_date.'" and create_dt<"'.$end_date.'"';
        $where3 = 's.create_dt>="'.$start_date.'" and s.create_dt<"'.$end_date.'"';
        $where4 = 'po.create_dt>="'.$start_date.'" and po.create_dt<"'.$end_date.'"';
        $where5 = 'u.jointime>='.$start_time.' and u.jointime<'.$end_time;
        $where6 = 'p.create_dt>="'.$start_date.'" and p.create_dt<"'.$end_date.'"';
        // 新增商家
        // $xzsj = $this->db2->name('shop')->where($where_city)->where($where_group2)->where(['shop_status'=>1])->where($where2)->count();
        // 新增有效店铺:有活动发布的店铺
        $xzyxdp = $this->db2->name('shop')->alias('s')->where(['s.shop_status'=>1,'s.is_auth'=>1])->where($where_city1)->where($where_group1)->where($where3)->count();
        $zxdp = $this->db2->name('shop')->alias('s')->join('chbc_play_order p','p.shop_no=s.shop_no')->where(['s.shop_status'=>1,'p.order_status'=>['in','3,-7,-8,-9']])->where($where_city1)->where($where_group1)->where($where6)->group('s.id')->count();
        // 优质店铺:满返条件差价小于或等于3的店铺
        // $zxyzdp = $this->db2->name('shop')->alias('s')->join('chbc_play p','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('s.shop_status=1 and p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->group('s.id')->count();
        // // 消耗名额
        $xhme = (int)$this->db2->name('play_order')->alias('po')->join('chbc_shop s','po.shop_no=s.shop_no')->where('po.order_status in (3,-9)')->where($where_city1)->where($where_group3)->where($where4)->count();
        // // 总名额
        // $zme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5)')->where($where6)->value('sum(p.num)');
        // if ($zme==0) {
        //     $xhmezme=0;
        // } else {
        //     $xhmezme = round(100*$xhme/$zme,2);
        // }
        // 优质名额消耗
        // $yzmexh = (int)$this->db2->name('play')->alias('p')->join('chbc_play_order po','po.play_no=p.play_no')->join('chbc_shop s','po.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('po.order_status=3 and p.user_pay<=3+p.back_money')->where($where6)->count();
        // // 优质名额:满返条件差价小于或等于3的活动
        // $yzme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->value('sum(p.num)');
        // $yzmexhl = 0;
        // if ($yzme!=0) {
        //     $yzmexhl = round(100*$yzmexh/$yzme,2);
        // }
        // 系统利润
        // $xtlr = (float)$this->db2->name('play_order')
        //     ->alias('po')
        //     // ->join('chbc_play p','po.play_no=p.play_no')
        //     ->join('chbc_shop s','po.shop_no=s.shop_no')
        //     ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group3)->where($where4)->value('sum(po.service_charge_amt)');
        // // 经纪人收益
        // $middleman = $this->db2->name('middleman')->field('id,rebate_amt')->select();
        // $rebate_amt = [];
        // foreach ($middleman as $key => $value) {
        //     $rebate_amt[$value['id']] = $value['rebate_amt'];
        // }
        // $orders = $this->db2->name('play_order')
        //     ->alias('po')
        //     //->join('chbc_play p','po.play_no=p.play_no')
        //     ->join('chbc_shop s','po.shop_no=s.shop_no')
        //     ->where(['po.order_status'=>['in','3,6,-7,-8,-9']])->where($where_city1)->where($where_group3)->where('s.middleman_id>0')->where($where4)->field('s.service_amt,s.middleman_id')->select();
        // $jjrsy = 0;
        // if ($orders) {
        //     foreach ($orders as $key => $value) {
        //         if (isset($rebate_amt[$value['middleman_id']])) {
        //             $jjrsy+=$rebate_amt[$value['middleman_id']];
        //         }
        //     }
        // }
        // $jjrsy = round($jjrsy,2);
        // // 实际收益
        // $sjsy = round($xtlr-$jjrsy,2);
        // 待支付金额
        $dzfje = (float)$this->db2->name('play_order')
            ->alias('po')
            //->join('chbc_play p','po.play_no=p.play_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,-9'],'po.is_pay'=>0])->where($where_city1)->where($where_group3)->where('po.create_dt<"'.$start_date.'"')->value('sum(po.service_charge_amt+po.actual_rebate_amt)');
        // $ptfwf = (float)$this->db2->name('play_order')
        //     ->alias('po')
        //     ->join('chbc_shop s','po.shop_no=s.shop_no')
        //     ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group3)->where($where4)->count();
        // $ptfwf = round($ptfwf/2,1);
        $jrgk = [
            // 'xzsj' => $xzsj,
            'xzyxdp' => $xzyxdp,
            'zxdp' => $zxdp,
            'xhme' => $xhme,
            // 'zxyzdp' => $zxyzdp,
            // 'mexh' => $xhme.'/'.$zme.' ('.$xhmezme.'%)',
            // // 'yzmexhl' => $yzmexh.'/'.$yzme,
            // 'jjrsy' => $jjrsy,
            // 'sjsy' => $sjsy,
            'dzfje' => $dzfje,
            // 'ptfwf' => $ptfwf,
        ];
        $salesmanList = $this->salesmanList();
        // 当前用户所属用户组
        $auth_group_access = $this->db2->name('auth_group_access')->where(['uid'=>$this->auth->id])->order('group_id asc')->find();
        $this->view->assign("jrgk", $jrgk);
        $this->view->assign("salesmanList", $salesmanList);
        $this->view->assign("auth_group_access", $auth_group_access);
        // 销单
        // $channels = $this->db2->name('channel')->where('status',1)->field('id,name')->select();
        // foreach ($channels as $key => &$value) {
        //     if ($value['name']=='吃喝不愁') {
        //         // 已销单
        //         $yxd = (int)$this->db2->name('play_order')
        //         ->alias('po')
        //         ->join('chbc_shop s','po.shop_no=s.shop_no')
        //         ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1,'is_yunzhan'=>0])->where($where_city1)->where($where_group3)->where($where4)->count();
        //     } else {
        //         // 已销单
        //         $yxd = (int)$this->db2->name('play_order')
        //         ->alias('po')
        //         ->join('chbc_shop s','po.shop_no=s.shop_no')
        //         ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1,'is_yunzhan'=>$value['id']])->where($where_city1)->where($where_group3)->where($where4)->count();
        //     }
        //     // 总量
        //     $quota = 0;
        //     if ($value['name']=='歪麦') {
        //         $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel')->select();
        //         foreach ($zl as $z) {
        //             $channel = json_decode($z['channel'],1);
        //             foreach ($channel as $c) {
        //                 if ($c['channel_id']==$value['id']) {
        //                     $quota+=$c['quota'];break;
        //                 }
        //             }
        //         }
        //         $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%歪卖%"')->where($where6)->field('p.channel')->select();
        //         foreach ($zl as $z) {
        //             $channel = json_decode($z['channel'],1);
        //             foreach ($channel as $c) {
        //                 if ($c['channel_id']==$value['id']) {
        //                     $quota+=$c['quota'];break;
        //                 }
        //             }
        //         }
        //     } else {
        //         $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel')->select();
        //         foreach ($zl as $z) {
        //             $channel = json_decode($z['channel'],1);
        //             foreach ($channel as $c) {
        //                 if ($c['channel_id']==$value['id']) {
        //                     $quota+=$c['quota'];break;
        //                 }
        //             }
        //         }
        //     }
        //     // 百分比
        //     if ($quota==0) {
        //         $xdbfb = 0;
        //     } else {
        //         $xdbfb = round(100*$yxd/$quota,2);
        //     }
        //     $value['yxd'] = $yxd;
        //     $value['zl'] = $quota;
        //     $value['xdbfb'] = $xdbfb;
        // }
        // $this->view->assign("channels", $channels);
        return $this->view->fetch();
    }

    /**
     * 业务员排名
     * @return [type] [description]
     */
    public function salesman()
    {

        $salesman = input('salesman','');
        // 当前用户的权限
        // if ($salesman) {
        //     $auth_group_access = Db::name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman])->find();
        //     $groups = [];
        //     if ($auth_group_access['group_id']==3) { // 业务员
        //         $groups[] = $salesman;
        //     } elseif ($auth_group_access['group_id']==5) { // 经理
        //         $groups[] = $salesman;
        //         $admin = Db::name('admin')->where(['leader'=>$salesman])->find();
        //         foreach ($admin as $key => $value) {
        //             $groups[] = $value['id'];
        //         }
        //     }
        // } else {
        //     $auth_group_access = Db::name('auth_group_access')->alias('aga')->where(['aga.uid'=>$this->auth->id])->find();
        //     $groups = [];
        //     if ($auth_group_access['group_id']==3) { // 业务员
        //         $groups[] = $this->auth->id;
        //     } elseif ($auth_group_access['group_id']==5) { // 经理
        //         $groups[] = $this->auth->id;
        //         $admin = Db::name('admin')->where(['leader'=>$this->auth->id])->find();
        //         foreach ($admin as $key => $value) {
        //             $groups[] = $value['id'];
        //         }
        //     }
        // }
        $time = time();
        $city = input('city','');
        $date = input('date','');
        $type = input('type','');
        $daybefore = strtotime(date('Y-m-d'));
        $start_date = date('Y-m-d',$time);
        $end_date = date('Y-m-d H:i:s',$time);
        $start_time = strtotime($start_date);
        $where_date = ' and create_dt>"'.$start_date.'" and create_dt<="'.$end_date.'"';
        // $where_date1 = '';
        $where_date1 = ' and s.create_dt>"'.$start_date.'" and s.create_dt<="'.$end_date.'"';
        $where_date2 = ' and p.create_dt>"'.$start_date.'" and p.create_dt<="'.$end_date.'"';
        $where_date3 = ' and po.create_dt>"'.$start_date.'" and po.create_dt<="'.$end_date.'"';
        $where_city = '';
        $where_city1 = '';
        $where_group = '';
        $where_group1 = '';
        // if ($groups) {
        //     $where_group = ' and serviceman_admin_id in ('.implode(',', $groups).')';
        //     $where_group1 = ' and s.serviceman_admin_id in ('.implode(',', $groups).')';
        // }
        if ($date) {
            list($start_date,$end_date) = explode(' - ', $date);
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
            $where_date = ' and create_dt>"'.$start_date.'" and create_dt<="'.$end_date.'"';
            $where_date1 = ' and s.create_dt>"'.$start_date.'" and s.create_dt<="'.$end_date.'"';
            $where_date2 = ' and p.create_dt>"'.$start_date.'" and p.create_dt<="'.$end_date.'"';
            $where_date3 = ' and po.create_dt>"'.$start_date.'" and po.create_dt<="'.$end_date.'"';
        }
        if ($city) {
            $citys = explode('/', $city);
            if (count($citys)==3) {
                $end = end($citys);
                $where_city = ' and area="'.$end.'"';
                $where_city1 = ' and s.area="'.$end.'"';
            } elseif (count($citys)==2) {
                $end = end($citys);
                $where_city = ' and city="'.$end.'"';
                $where_city1 = ' and s.city="'.$end.'"';
            } else {
                $end = end($citys);
                $where_city = ' and province="'.$end.'"';
                $where_city1 = ' and s.province="'.$end.'"';
            }
        }
        $sql = 'select salesman_admin_id,count(salesman_admin_id) as count from chbc_shop where shop_status=1 '.$where_date.$where_city.$where_group.' group by salesman_admin_id order by count DESC limit 0,10';
        if ($type==2) {
            $sql = 'select a.salesman_admin_id,count(a.salesman_admin_id) as count from (select s.salesman_admin_id from chbc_shop s left join chbc_play p on s.shop_no=p.shop_no where s.shop_status=1 '.$where_city1.$where_group1.$where_date2.' group by s.shop_no) a group by a.salesman_admin_id order by count DESC limit 0,10';
        } elseif ($type==3) {
            // $sql = 'select sum(p.num) as num,sum(p.stock) as stock,s.salesman_admin_id,sum(p.stock)/sum(p.num) as rate from chbc_shop s left join chbc_play p on s.shop_no=p.shop_no where s.shop_status=1 '.$where_city.$where_date2.' group by salesman_admin_id order by rate asc';
            $sql = 'select count(po.id) as count,s.salesman_admin_id from chbc_play_order po left join chbc_shop s on s.shop_no=po.shop_no where po.order_status=3 '.$where_city1.$where_group1.$where_date3.' group by s.salesman_admin_id order by count DESC limit 0,10';
            $ywypm = Db::query($sql);
            $arr = [];
            $h = 0;
            $s = 0;
            foreach ($ywypm as $key => $value) {
                $sales_man_name = Db::name('admin')->where('id',$value['salesman_admin_id'])->value('nickname');
                if ($sales_man_name) {
                    $value['sales_man_name'] = $sales_man_name;
                    $sql = 'select sum(num) as sum from chbc_play p left join chbc_shop s on s.shop_no=p.shop_no where p.play_status in (1,2,3,4,5) and s.salesman_admin_id='.$value['salesman_admin_id'].$where_city1.$where_group1.$where_date2;
                    $play = Db::query($sql);
                    $h+=$value['count'];
                    $value['count'] = $value['count'].'/'.$play[0]['sum'];
                    $arr[] = $value;
                    $s+=$play[0]['sum'];
                }
            }
            $arr[] = ['sales_man_name'=>'合计','count'=>$h.'/'.$s];
            
            $this->success('业务员排名','',$arr);
        }
        $ywypm = Db::query($sql);
        $arr = [];
        $h = 0;
        foreach ($ywypm as $key => $value) {
            $sales_man_name = Db::name('admin')->where('id',$value['salesman_admin_id'])->value('nickname');
            if ($sales_man_name) {
                $value['sales_man_name'] = $sales_man_name;
                $arr[] = $value;
                $h+=$value['count'];
            }
        }
        $arr[] = ['sales_man_name'=>'合计','count'=>$h];
        
        $this->success('业务员排名','',$arr);
    }

    /**
     * 今日概况筛选
     * @return [type] [description]
     */
    public function dailyOverview()
    {
        $city = input('city','');
        $date = input('date','');
        $salesman = input('salesman','');
        // 当前用户的权限
        if ($salesman) {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman])->order('group_id DESC')->find();
            $groups = [];
            if ($auth_group_access['group_id']==3 || $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman,'aga.group_id'=>3])->find()) { // 业务员
                $groups[] = $salesman;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $salesman;
                $admin = $this->db2->name('admin')->where(['leader'=>$salesman])->select();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        } else {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$this->auth->id])->order('group_id DESC')->find();
            $groups = [];
            if ($auth_group_access['group_id']==3 || $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman,'aga.group_id'=>3])->find()) { // 业务员
                $groups[] = $this->auth->id;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $this->auth->id;
                $admin = $this->db2->name('admin')->where(['leader'=>$this->auth->id])->select();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        }
        $where_group= [];
        $where_group1= [];
        $where_group2= [];
        $where_group3= [];
        if ($groups) {
            $where_group['service_admin_id'] = ['in',$groups];
            $where_group1['p.service_admin_id'] = ['in',$groups];// 涉及到业务员交接，用活动的字段查询
            $where_group2['salesman_admin_id'] = ['in',$groups];
            $where_group3['s.salesman_admin_id'] = ['in',$groups];
        }
        $where_city = [];
        $where_city1 = [];
        if ($city) {
            $citys = explode('/', $city);
            if (count($citys)==3) {
                $end = end($citys);
                $where_city['area'] = $end;
                $where_city1['s.area'] = $end;
            } elseif (count($citys)==2) {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            } else {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            }
        }
        // 今日概况
        $time = time();
        $start_date = date('Y-m-d',$time);
        $end_date = date('Y-m-d H:i:s',$time);
        $start_time = strtotime($start_date);
        $end_time = $time;
        if ($date) {
            list($start_date,$end_date) = explode(' - ', $date);
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
        }
        $where1 = 'jointime>='.$start_time.' and jointime<'.$end_time;
        $where2 = 'create_dt>="'.$start_date.'" and create_dt<"'.$end_date.'"';
        $where3 = 's.create_dt>="'.$start_date.'" and s.create_dt<"'.$end_date.'"';
        $where4 = 'po.create_dt>="'.$start_date.'" and po.create_dt<"'.$end_date.'"';
        $where5 = 'u.jointime>='.$start_time.' and u.jointime<'.$end_time;
        $where6 = 'p.create_dt>="'.$start_date.'" and p.create_dt<"'.$end_date.'"';
        // 新增商家
        // $xzsj = $this->db2->name('shop')->where($where_city)->where($where_group2)->where(['shop_status'=>1])->where($where2)->count();
        // 新增有效店铺:有活动发布的店铺
        $xzyxdp = $this->db2->name('shop')->alias('s')->where(['s.shop_status'=>1,'s.is_auth'=>1])->where($where_city1)->where($where_group3)->where($where3)->count();
        $zxdp = $this->db2->name('shop')->alias('s')->join('chbc_play_order p','p.shop_no=s.shop_no')->where(['s.shop_status'=>1,'p.order_status'=>['in','3,-7,-8,-9']])->where($where_city1)->where($where_group1)->where($where6)->group('s.id')->count();
        // 优质店铺:满返条件差价小于或等于3的店铺
        // $zxyzdp = $this->db2->name('shop')->alias('s')->join('chbc_play p','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('s.shop_status=1 and p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->group('s.id')->count();
        // // 消耗名额
        $xhme = (int)$this->db2->name('play_order')->alias('po')->join('chbc_shop s','po.shop_no=s.shop_no')->where('po.order_status in (3,-9) and po.is_effect=1')->where($where_city1)->where($where_group3)->where($where4)->count();
        // // 总名额
        // $zme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5)')->where($where6)->value('sum(p.num)');
        // if ($zme==0) {
        //     $xhmezme=0;
        // } else {
        //     $xhmezme = round(100*$xhme/$zme,2);
        // }
        // 优质名额消耗
        // $yzmexh = (int)$this->db2->name('play')->alias('p')->join('chbc_play_order po','po.play_no=p.play_no')->join('chbc_shop s','po.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('po.order_status=3 and p.user_pay<=3+p.back_money')->where($where6)->count();
        // 优质名额:满返条件差价小于或等于3的活动
        // $yzme = (int)$this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.play_status in (1,2,3,4,5) and p.user_pay<=3+p.back_money')->where($where6)->value('sum(p.num)');
        // $yzmexhl = 0;
        // if ($yzme!=0) {
        //     $yzmexhl = round(100*$yzmexh/$yzme,2);
        // }
        // 系统利润
        // $xtlr = (float)$this->db2->name('play_order')
        //     ->alias('po')
        //     //->join('chbc_play p','po.play_no=p.play_no')
        //     ->join('chbc_shop s','po.shop_no=s.shop_no')
        //     ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group3)->where($where4)->value('sum(po.service_charge_amt)');
        // // 经纪人收益
        // $middleman = $this->db2->name('middleman')->field('id,rebate_amt')->select();
        // $rebate_amt = [];
        // foreach ($middleman as $key => $value) {
        //     $rebate_amt[$value['id']] = $value['rebate_amt'];
        // }
        // $orders = $this->db2->name('play_order')
        //     ->alias('po')
        //     //->join('chbc_play p','po.play_no=p.play_no')
        //     ->join('chbc_shop s','po.shop_no=s.shop_no')
        //     ->where(['po.order_status'=>['in','3,6,-7,-8,-9']])->where($where_city1)->where($where_group3)->where('s.middleman_id>0')->where($where4)->field('s.service_amt,s.middleman_id')->select();
        // $jjrsy = 0;
        // if ($orders) {
        //     foreach ($orders as $key => $value) {
        //         if (isset($rebate_amt[$value['middleman_id']])) {
        //             $jjrsy+=$rebate_amt[$value['middleman_id']];
        //         }
        //     }
        // }
        // $jjrsy = round($jjrsy,2);
        // // 实际收益
        // $sjsy = round($xtlr-$jjrsy,2);
        // 待支付金额
        $dzfje = (float)$this->db2->name('play_order')
            ->alias('po')
            //->join('chbc_play p','po.play_no=p.play_no')
            ->join('chbc_shop s','po.shop_no=s.shop_no')
            ->where(['po.order_status'=>['in','3,-9'],'po.is_pay'=>0])->where($where_city1)->where($where_group3)->where('po.create_dt<"'.date('Y-m-d',$time).'"')->value('sum(po.service_charge_amt+po.actual_rebate_amt)');
        // 平台服务费
        // $ptfwf = (float)$this->db2->name('play_order')
        //     ->alias('po')
        //     ->join('chbc_shop s','po.shop_no=s.shop_no')
        //     ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1])->where($where_city1)->where($where_group3)->where($where4)->count();
        // $ptfwf = round($ptfwf/2,1);

        $jrgk = [
            // 'xzsj' => $xzsj,
            'xzyxdp' => $xzyxdp,
            'zxdp' => $zxdp,
            'xhme' => $xhme,
            // 'zxyzdp' => $zxyzdp,
            // 'mexh' => $xhme.'/'.$zme.' ('.$xhmezme.'%)',
            // // 'yzmexhl' => $yzmexh.'/'.$yzme,
            // 'jjrsy' => $jjrsy,
            // 'sjsy' => $sjsy,
            'dzfje' => $dzfje,
            // 'ptfwf' => $ptfwf,
        ];
        $this->success('今日概况','',$jrgk);
    }

    /**
     * 销单
     * @return [type] [description]
     */
    public function salesOrder()
    {
        $city = input('city','');
        $date = input('date','');
        $salesman = input('salesman','');
        // 当前用户的权限
        if ($salesman) {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman])->order('group_id DESC')->find();
            $groups = [];
            if ($auth_group_access['group_id']==3 || $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman,'aga.group_id'=>3])->find()) { // 业务员
                $groups[] = $salesman;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $salesman;
                $admin = $this->db2->name('admin')->where(['leader'=>$salesman])->select();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        } else {
            $auth_group_access = $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$this->auth->id])->order('group_id DESC')->find();
            $groups = [];
            if ($auth_group_access['group_id']==3 || $this->db2->name('auth_group_access')->alias('aga')->where(['aga.uid'=>$salesman,'aga.group_id'=>3])->find()) { // 业务员
                $groups[] = $this->auth->id;
            } elseif ($auth_group_access['group_id']==5) { // 经理
                $groups[] = $this->auth->id;
                $admin = $this->db2->name('admin')->where(['leader'=>$this->auth->id])->select();
                foreach ($admin as $key => $value) {
                    $groups[] = $value['id'];
                }
            }
        }
        $where_group= [];
        $where_group1= [];
        $where_group2= [];
        $where_group3= [];
        if ($groups) {
            $where_group['service_admin_id'] = ['in',$groups];
            $where_group1['p.service_admin_id'] = ['in',$groups];// 涉及到业务员交接，用活动的字段查询
            $where_group2['salesman_admin_id'] = ['in',$groups];
            $where_group3['s.salesman_admin_id'] = ['in',$groups];
        }
        $where_city = [];
        $where_city1 = [];
        if ($city) {
            $citys = explode('/', $city);
            if (count($citys)==3) {
                $end = end($citys);
                $where_city['area'] = $end;
                $where_city1['s.area'] = $end;
            } elseif (count($citys)==2) {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            } else {
                $end = end($citys);
                $where_city['city'] = $end;
                $where_city1['s.city'] = $end;
            }
        }
        // 今日概况
        $time = time();
        $start_date = date('Y-m-d',$time);
        $end_date = date('Y-m-d H:i:s',$time);
        $start_time = strtotime($start_date);
        $end_time = $time;
        if ($date) {
            list($start_date,$end_date) = explode(' - ', $date);
            $start_time = strtotime($start_date);
            $end_time = strtotime($end_date);
        }
        $where1 = 'jointime>='.$start_time.' and jointime<'.$end_time;
        $where2 = 'create_dt>="'.$start_date.'" and create_dt<"'.$end_date.'"';
        $where3 = 's.create_dt>="'.$start_date.'" and s.create_dt<"'.$end_date.'"';
        $where4 = 'po.create_dt>="'.$start_date.'" and po.create_dt<"'.$end_date.'"';
        $where5 = 'u.jointime>='.$start_time.' and u.jointime<'.$end_time;
        $where6 = 'p.create_dt>="'.$start_date.'" and p.create_dt<"'.$end_date.'"';
        // 销单
        $channels = $this->db2->name('channel')->where('status',1)->field('id,name')->select();
        foreach ($channels as $key => &$value) {
            if ($value['name']=='吃喝不愁') {
                // 已销单
                $yxd = (int)$this->db2->name('play_order')
                ->alias('po')
                ->join('chbc_shop s','po.shop_no=s.shop_no')
                ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1,'is_yunzhan'=>0])->where($where_city1)->where($where_group3)->where($where4)->count();
            } else {
                // 已销单
                $yxd = (int)$this->db2->name('play_order')
                ->alias('po')
                ->join('chbc_shop s','po.shop_no=s.shop_no')
                ->where(['po.order_status'=>['in','3,6,-7,-8,-9'],'po.is_effect'=>1,'is_yunzhan'=>$value['id']])->where($where_city1)->where($where_group3)->where($where4)->count();
            }
            // 总量
            $quota = 0;
            if ($value['name']=='歪麦') {
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $quota+=$c['quota'];break;
                        }
                    }
                }
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%歪卖%"')->where($where6)->field('p.channel')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $quota+=$c['quota'];break;
                        }
                    }
                }
            } else {
                $zl = $this->db2->name('play')->alias('p')->join('chbc_shop s','p.shop_no=s.shop_no')->where($where_city1)->where($where_group1)->where('p.channel like "%'.$value['name'].'%"')->where($where6)->field('p.channel')->select();
                foreach ($zl as $z) {
                    $channel = json_decode($z['channel'],1);
                    foreach ($channel as $c) {
                        if ($c['channel_id']==$value['id']) {
                            $quota+=$c['quota'];break;
                        }
                    }
                }
            }
            // 百分比
            if ($quota==0) {
                $xdbfb = 0;
            } else {
                $xdbfb = round(100*$yxd/$quota,2);
            }
            $value['yxd'] = $yxd;
            $value['zl'] = $quota;
            $value['xdbfb'] = $xdbfb;
        }
        $this->success('今日概况','',$channels);
    }
}
